package com.janwen.j2se.string;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class StringFormat {

	private static final Log logger = LogFactory.getLog(StringFormat.class);

	public static void formatString(String source, Object... args) {
		print(String.format("Hi %s, you owe me $%5.2f.", "Jack", 25.));

	}

	/**
	 * 
	 * janwen %[argument_index$][flags][width][.precision]conversion
	 * 
	 * 必须以%开头,$结束
	 */
	public static void testArgumentIndex() {
		print(String.format("nubmer \"%1$s\" %2$d", 10, 20));
	};

	/**
	 * Unindexed format specifiers take argument values from the argument list
	 * by the order they appear in the format string janwen
	 * 没有%指定索引位置的specifier按照参数的顺序填充
	 */
	public static void testArgumentIndex2() {
		// 第一个%d用10,最后一个%d用40
		print(String.format(" %d \"%1$s\" %2$d %d,%d,%d", 10, 20, 30, 40));
	};

	/**
	 * %n,%% janwen
	 * 
	 */
	public static void testMeta() {
		print(String.format("first line %n;newline%%"));
	}

	public static void testGeneral() {
		print(
		// any object can be formatted as string
		// upper case S converts string to upper case
		String.format("%s, %S, %S, %S, %s", "String", "String", null, (byte) 1,
				5.6));

		print(
		// any object can be formatted as boolean
		// upper case B prints TRUE or FALSE
		String.format("%b, %B, %b, %B", "String", null, (byte) 1, 5.6));

		print(
		// any object can be formatted as hex
		// upper case H prints hex in uppercase
		String.format("%h, %H, %H, %h, %H", "161", null, 161, new Integer(161),
				5.6));

		// What's the effect of width.precision on String?
		print(String.format("\"%1$s\", \"%1$14s\", \"%1$14.2s\"", "Hello"));
	}

	public static void testCharacter() {
		print(String.format("'%1$s', '%1$c', '%1$C'", 97));
	}

	public static void testInteger() {
		print(String.format("%d, %o, %h, %H", 161, 161, 161, 161));

		// try big number with and without group separator
		print(String.format("%1$d, %1$,d", 161161161161L));
	}

	public static void testFloat() {
		print(String.format("%1$.2e, %1$.2f, %1$.2g, %1$.2a", 12345678.9999932));
	}

	public static void testDate() {
		long currentTime = System.currentTimeMillis();
		java.util.Date date = new java.util.Date(currentTime);

		print(String.format(
				"Current Time: %1$tm/%1$td/%1$tY %1$tH:%1$tM:%1$tS",
				currentTime));

		// same as above but using shorthand notation
		print(String.format(
				"Current Time (using composition suffix): %1$tD %1$tT",
				currentTime));

		print(String
				.format("Current Time (using Date object): %1$tm/%1$td/%1$tY %1$tH:%1$tM:%1$tS",
						date));
	}

	public static void testLocaleDate() {
		long currentTime = System.currentTimeMillis();
		java.util.Date date = new java.util.Date(currentTime);

		print(String.format(
				"Current Time: %1$tB/%1$ta/%1$tY %1$tH:%1$tM:%1$tS",
				currentTime));

		print(String.format(
				"Current Time: %1$tb/%1$td/%1$tY %1$tH:%1$tM:%1$tS",
				currentTime));

		// same as above but using shorthand notation
		print(String.format(
				"Current Time (using composition suffix): %1$tD %1$tT",
				currentTime));

		print(String
				.format("Current Time (using Date object): %1$tm/%1$td/%1$tY %1$tH:%1$tM:%1$tS",
						date));
	}

	public static void testFlags() {
		print(String.format(
				"'%1$s', '%1$#s', '%1$-10.8s', '%1$.12s', '%1$-25s'",
				"Huge Fruit, Inc."));
	}

	
	public static void testNumFlags() {
		print(String.format(
				"'%1$s', '%1$#s', '%1$-5.8d', '%1$.12s', '%1$-25s'",
				1000.1234567890));
	}
	
	
	public static void testPrecision(){
		print(String.format("%1$3.5f", 111f));
	}
	static void print(String str) {
		logger.info(str);
	}
}
